package com.jdy.utils;

import java.io.Closeable;
import java.sql.Blob;
import java.sql.Clob;

/**
 * Spring 框架学习
 * <p>
 * [释放资源工具类]
 * <p>
 * 创建人 Dale 时间 2020/3/9 20:33
 */
public class CloseUtil {

    /**
     * 关闭autoCloseable的对象，释放资源
     *
     * @param autoCloseable 自动关闭资源接口
     */
    public static void close(AutoCloseable autoCloseable) {
        if (autoCloseable == null) {
            return;
        }

        try {
            autoCloseable.close();//释放资源
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 关闭Closeable的对象
     *
     * @param closeable 关闭资源接口
     */
    public static void close(Closeable closeable) {
        if (closeable == null) {
            return;
        }

        try {
            closeable.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void close(Blob blob) {
        if (blob == null) {
            return;
        }

        try {
            blob.free();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void close(Clob clob) {
        if (clob == null) {
            return;
        }

        try {
            clob.free();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
